package cn.shyouchi.wuguanjia.android.ui.event.rank;

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import cn.shyouchi.wuguanjia.android.widget.dialog.TimePickerDialogFragment;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddRankActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.edit_rank_name)
    EditText mEditRankName;
    @BindView(R.id.tv_rank_start)
    TextView mTvRankStart;
    @BindView(R.id.tv_rank_end)
    TextView mTvRankEnd;


    EventApi mEventApi;
    String projectId;

    EventRankEntity.ResultMessageBean entity;


    boolean isEdit = false;

    @Override
    public int initContentView() {
        return R.layout.activity_add_rank;
    }

    @Override
    public void initInjector() {
        mEventApi = new EventApi(mContext);
        projectId = getIntent().getStringExtra("projectId");
        entity = getIntent().getParcelableExtra("rank");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        if (entity != null) {
            isEdit = true;
            ToastUtil.showToast("编辑状态");
            initModule();
        }
    }

    private void initModule() {
        mEditRankName.setText(entity.getServicesname());
        mTvRankStart.setText(entity.getStarttime());
        mTvRankEnd.setText(entity.getEndtime());

        mTvRankStart.setTag(1);
        mTvRankEnd.setTag(1);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsAction(2);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {
            generateEntityExit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void generateEntityExit() {
        if (TextUtils.isEmpty(mEditRankName.getText().toString())) {
            mEditRankName.setError("标题不能空");
            return;
        }


        if (mTvRankStart.getTag() == null) {
            ToastUtil.showToast("请设置开始时间");
            return;

        }
        if (mTvRankEnd.getTag() == null) {
            ToastUtil.showToast("请设置结束时间");
            return;
        }

//        SimpleDateFormat sdf = new SimpleDateFormat();
//        try {

        if (isEdit) {
            mEventApi.upRank(projectId, String.valueOf(entity.getId()), mEditRankName.getText().toString(),
                    mTvRankStart.getText().toString(),
                    mTvRankEnd.getText().toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            EventBus.getDefault().post(new DataChangeEvent());
                            finish();
                        }
                    });


        } else {
            mEventApi.addRank(projectId, mEditRankName.getText().toString(),
                    mTvRankStart.getText().toString(),
                    mTvRankEnd.getText().toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            setResult(EventRankActivity.REQUEST_TYPE_ADD_SUCCESS);
                            finish();
                        }
                    });
        }



//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
    }


    @OnClick({R.id.tv_rank_start, R.id.tv_rank_end})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_rank_start:
                showTimePicker(mTvRankStart, "开始时间");
                break;
            case R.id.tv_rank_end:
                showTimePicker(mTvRankEnd, "结束时间");
                break;
        }
    }


    public void showTimePicker(final TextView tv, String title) {
        TimePickerDialogFragment mTimePickerDialog;
        mTimePickerDialog = new TimePickerDialogFragment(getSupportFragmentManager(), title, "");
        mTimePickerDialog.setOnTimePickedListener(new TimePickerDialogFragment.onTimePickedListener() {
            @Override
            public void onPicked(String time) {
                tv.setText(time);
                tv.setTag(1);
            }
        });
        mTimePickerDialog.showDialog();
    }

}
